package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    private final String PATTERN = "dish_*";

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 分页查询菜品
     * @param dishPageQueryDTO  传入的菜品分页参数
     * @return  返回分页查询到的数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> dishPage(DishPageQueryDTO dishPageQueryDTO ){
        log.info("传入的分页参数为，{}",dishPageQueryDTO);

        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品停售起售
     * @return  返回操作结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "停售起售")
    public Result status(@PathVariable Integer status, Long id){

        dishService.status(status,id);

        //清理redis缓存
        cleanCache(PATTERN);
        return Result.success();
    }

    /**
     * 删除菜品
     * @return  返回操作结果
     */
    @DeleteMapping
    @ApiOperation(value = "删除菜品")
    public Result delDishes(@RequestParam List<Long> ids){
        log.info("传入的用户id是，{}",ids);
        dishService.delDishes(ids);
        log.info("菜品删除成功！删除的菜品id为：{}",ids);
        //清理redis缓存
        cleanCache(PATTERN);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id    要查询的菜品id
     * @return  返回操作结果
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("传入的id是，{}",id);

        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        dishService.save(dishDTO);
        //清理redis缓存
        cleanCache(PATTERN);
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO){

        dishService.updateDish(dishDTO);
        //清理redis缓存
        cleanCache(PATTERN);
        return Result.success();
    }

    /**
     * 根据分类查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据分类查询菜品")
    public Result<List<DishVO>> getByCategoryId( Long categoryId){
        log.info("id{}",categoryId);
        return Result.success(dishService.getByCategoryId(categoryId));

    }

    /**
     * 清理redis缓存
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
        log.info("缓存清理成功");
    }
}
